package com.feib.stms.util;




import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import org.apache.commons.io.FileUtils;
import org.springframework.util.Assert;

import com.feib.stms.model.MailQueue;





public class MailContentTemplate
{
    /**
     * 信件標題
     */
    private String messageTile;
    
    /**
     * 段落內容
     */
    private String[] paragraphs;
    
    private String htmlMailFolder;
    
    public MailContentTemplate(String messageTile, String[] paragraphs, String htmlMailFolder)
    {
        this.messageTile = messageTile;
        this.paragraphs = paragraphs;
        Assert.notNull(htmlMailFolder, "htmlMailFolder 不可以為NULL");
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        this.htmlMailFolder = htmlMailFolder + dateFormat.format(new Date())+ File.separator;
    }
    
    public MailQueue createMailQueue() throws IOException
    {
        MailQueue mailQueue = new MailQueue();
        
        mailQueue.setMailSubject(messageTile);

        FileUtils.forceMkdir(new File(htmlMailFolder));
        
        String contentFileName = htmlMailFolder + "mailContent-" +UUID.randomUUID().toString() + ".html";
        
        // 將資料寫入檔案 (因為有可能太大，資料庫無法儲存)
        FileUtils.writeStringToFile(new File(contentFileName), getContent(), "UTF-8");
        mailQueue.setMailContent(contentFileName);
        
        StringBuffer inlineResource = new StringBuffer();
        inlineResource.append("banner.jpg:banner.jpg;");
        inlineResource.append("footerLogo.jpg:footerLogo.jpg;");
        mailQueue.setInlineResource(inlineResource.toString());

        mailQueue.setMailStatus(new Integer(0)); // 待發送
        mailQueue.setInQueueDate(new Date());
        
        inlineResource = null;
        
        return mailQueue;
    }
    
    protected String getContent()
    {
        StringBuffer html = new StringBuffer();
        
        html.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
        html.append("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
        html.append("<head>");
        html.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />");
        html.append("<title>亞證網銀交割款扣撥系統</title>");
        html.append("</head>");
        html.append("");
        html.append("<body>");
        html.append("<div style=\"width: 750px; margin: auto;\">");
        html.append("");
        html.append("    <a href=\"首頁/\"><img src=\"cid:banner.jpg\" style=\"border:none;\" /></a>");
        html.append("");
        html.append("   <div style=\"border: 1px dotted #C69595; width: 710px; margin: 10px 0; padding: 5px 20px 20px;\">");
        html.append("       <h3 style=\"background: #f4e3e3; border-bottom: 2px solid #C69595; text-align:center; padding: 2px 2px 2px 8px; font-family: calibri, 微軟正黑體;\">").append(null == messageTile ? "" :messageTile).append("</h3>");
        html.append("        <div style=\"line-height: 150%; padding: 0 20px; font-size: 14px; font-family: calibri, 微軟正黑體; color: #333;\">");
        
        getMailContent(html);        
        
        html.append("        </div>");
        html.append("   </div>");
        html.append("");    
        html.append("    <div style=\"text-align: center; padding: 10px 0; border-top: 1px dotted #C69595;\">");
        html.append("           <a href=\"http://www.feib.com.tw/\">");
        html.append("           <span style=\"font-size: 13px; font-family: calibri; color: #666;\">版權所有 &copy; 2012 Far Eastern International Bank. All Rights Reserved.</span>");
        html.append("    </div>");
        html.append("");
        html.append("</div>");
        html.append("</body>");
        html.append("</html>");
        
        return html.toString();
    }

    /**
     * 
     */
    protected StringBuffer getMailContent(StringBuffer html) {

        if (null != paragraphs && 0 != paragraphs.length)
        {
            for(String paragraph :paragraphs)
                html.append("           <p>").append(paragraph).append("</p>");
        }
        
        html = getOtherContent(html);
        
        return html;
    }

    /**
     * @param html
     * @return
     */
    protected StringBuffer getOtherContent(StringBuffer html) {
        return html;
    }

}
